/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qdu.impl;

import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author SBH
 */
public class BookPageImpl {

    ResultSet rs = null;
    int EVERYPAGENUM = 2;
    int count = -1;

    public void setEVERYPAGENUM(int EVERYPAGENUM) {
        this.EVERYPAGENUM = EVERYPAGENUM;
    }

    public int getMessageCount(String sql) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            con.commit();
            rs.next();
            count = rs.getInt(1);
            System.out.println("count=" + count);
            return count;

        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }

    }
    
    
    public int getPageCount() {
        if (count % EVERYPAGENUM == 0) {
            System.out.println("count/EVERYPAGENUM=" + (count / EVERYPAGENUM));
            return count / EVERYPAGENUM;
        } else {
            System.out.println("count/EVERYPAGENUM=" + (count / EVERYPAGENUM));
            return count / EVERYPAGENUM + 1;
        }
    }
    
    
    public List getMessage(int page, String sql) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List list = new ArrayList();
        try{
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
             for (int i = 0; i < (page - 1) * EVERYPAGENUM; i++) {
                rs.next();
            }
            for (int t = 0; t < EVERYPAGENUM; t++) {
                if (rs.next()) {
                    // 数组容器类对象-中间对象
                    List list2 = new ArrayList();
                    // 获取此显示信息内容
                    list2.add(rs.getString(1));
                    list2.add(rs.getString(2));
                    list2.add(rs.getString(4));
                    list2.add(rs.getString(5));
                    list2.add(rs.getString(6));
                    list2.add(rs.getString(7));
                    list2.add(rs.getString(8));
                    list2.add(rs.getString(9));
                    list2.add(rs.getString(10));
                    list2.add(rs.getString(11));
                    list2.add(rs.getString(12));
                    list2.add(rs.getString(13));
                    // 添加此显示信息到信息库
                    list.add(list2);
                } else {
                    // 减少空循环的时间
                    break;
                }
            }
            // 返回信息库
            return list;
        }catch (Exception e) {
            e.printStackTrace();
            return list;
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
    }

}
